package com.chookss.tiku.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.chookss.R
import com.chookss.tiku.entity.LeftEntiry
import com.chookss.tools.Utils
import com.johnrambo.ktea.ktExtends.onclick
import kotlinx.android.synthetic.main.item_right.view.*
import kotlinx.android.synthetic.main.item_right.view.ll_item_bg
import kotlinx.android.synthetic.main.item_right.view.tvSubjectNum


class RightPracticeListAdapter : RecyclerView.Adapter<ViewHolder>() {
    private var datas: List<LeftEntiry>? = null

    //private var secondList: List<LeftEntiry>? = null

    var listener: ((String) -> Unit)? = null
    private var selectParentCode: String? = null
    private val itemsList = mutableListOf<String>()

    override fun getItemCount() = datas?.size ?: 0
    override fun getItemViewType(position: Int) = datas!![position].lvlNo

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        if (viewType == 1) {
            return object : ViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_right_title, parent, false)
            ) {}
        } else {
            return object : ViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.item_right, parent, false)
            ) {}
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val entity = datas!![position]

        if (entity.lvlNo == 1) {
            if (entity.childList.isNotEmpty()) {
                (holder.itemView as TextView).text = entity.catalogName
            } else {
                /*
                To Goodsong:这里需求略有变化，为：
                       若2级下面 没有3级的chiltList，则把2级直接展示成3级的样式（即带熟练度的3级样式）
                       产品的意思为：该控件可以同时适应2级、3级的需求

                       ps:这里我与产品有些争议，我认为如果2级下面没有3级数据，则直接不展示2级，即：(holder.itemView as TextView).text = ""；
                          如果强制把将2级数据做为3级数据，则会打乱程序的逻辑性

                          你判断一下，我说的是否正确，if(不会打乱程序的逻辑) 则改之 else 我再与产品撕逼……

                 */
                (holder.itemView as TextView).text = ""
            }

        } else {
            holder.itemView.run {
                tvRight.text = entity.catalogName
                tvSubjectNum.text =""+ Utils.changeNumber(entity.subjectNum)+"道"
                tvAnswerNum.text ="今日已做"+ Utils.changeNumber(entity.answerNum)+"道"
                pbRight.progress=Utils.changeNumber(entity.proficiency)
                tvProficiency.text = "熟练度${entity.proficiency}%"
                if (entity.isSelected == true&&itemsList.contains(entity.catalogCode)) {

                    ll_item_bg.setBackgroundColor(Color.parseColor("#10E12416"))
                } else {
                    ll_item_bg.setBackgroundColor(Color.parseColor("#ffffff"))
                }

                onclick {
                    if (selectParentCode == entity.parentCode || itemsList.isEmpty()) {
                        selectParentCode = entity.parentCode
                        if (datas!![position].isSelected&&itemsList.contains(entity.catalogCode)) {
                            datas!![position].isSelected = false
                            itemsList.remove(entity.catalogCode)
                        } else {
                            datas!![position].isSelected = true
                            itemsList.add(entity.catalogCode)
                        }
                        notifyDataSetChanged()

                        listener?.let {
                            it(itemsList.toString())
                            //Log.i("catalogCodes","catalogCodes:$itemsList")
                        }
                    } else {
                        selectParentCode = entity.parentCode
                        itemsList.clear()

                        datas!![position].isSelected = true
                        itemsList.add(entity.catalogCode)
                        notifyDataSetChanged()

                        listener?.let {
                            it(itemsList.toString())
                            //Log.i("catalogCodes","catalogCodes:$itemsList")
                        }
                    }

                }
                //Log.i("itemsList",itemsList.toString())
            }
        }

    }


    fun update(list: List<LeftEntiry>) {
        datas = list
        itemsList.clear()
        notifyDataSetChanged()
    }


/*override fun getItemCount() {

}*/
}